package cn.inno.system;

import cn.inno.controller.BaseController;
import cn.inno.entitys.SysAdmin;
import cn.inno.entitys.SysAside;
import cn.inno.entitys.SysBase;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 系统配置控制器
 *
 * @author LeungZengJian
 */
@Controller
@RequestMapping("/system")
public class SystemController extends BaseController {

    @Autowired
    private SystemService systemService;

    @GetMapping("/system-base.html")
    public String baseEdit(Model model) {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin sysAdmin = (SysAdmin) this.getAuth();
            jsonObject = this.systemService.baseEdit();
            model.addAttribute(KEY_DATA, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/system/system-base";
    }

    @PostMapping("/baseUp")
    @ResponseBody
    public String baseUp(SysBase sysBase) {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin sysAdmin = (SysAdmin) this.getAuth();
            jsonObject = this.systemService.baseUp(sysBase);
        } catch (Exception e) {
            e.printStackTrace();
            this.writeExceptionJson(jsonObject, e);
        }
        return this.writeJson(jsonObject);
    }

    @GetMapping("/base")
    @ResponseBody
    public String getBase() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = this.systemService.baseEdit();
        } catch (Exception e) {
            e.printStackTrace();
            this.writeExceptionJson(jsonObject, e);
        }
        return this.writeJson(jsonObject);
    }

    @GetMapping("/categoryList")
    @ResponseBody
    public String categoryList() {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin sysAdmin = (SysAdmin) this.getAuth();
            jsonObject = this.systemService.categoryList(sysAdmin.getAdminId());
        } catch (Exception e) {
            this.writeExceptionJson(jsonObject, e);
        }
        return this.writeJson(jsonObject);
    }

    @PostMapping("/categoryAdd")
    @ResponseBody
    public String categoryAdd(SysAside sysAside) {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin sysAdmin = (SysAdmin) this.getAuth();
            jsonObject = this.systemService.categoryAdd(sysAside, sysAdmin.getAdminId());
        } catch (Exception e) {
            this.writeExceptionJson(jsonObject, e);
        }
        return this.writeJson(jsonObject);
    }

    @PostMapping("/categoryUp")
    @ResponseBody
    public String categoryUp(SysAside sysAside) {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin sysAdmin = (SysAdmin) this.getAuth();
            jsonObject = this.systemService.categoryUp(sysAside, sysAdmin.getAdminId());
        } catch (Exception e) {
            this.writeExceptionJson(jsonObject, e);
        }
        return this.writeJson(jsonObject);
    }

    @PostMapping("/categoryDel")
    @ResponseBody
    public String categoryDel(String asideIds) {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin admin = (SysAdmin) this.getAuth();
            jsonObject = this.systemService.categoryDel(asideIds, admin.getAdminId());
        } catch (Exception e) {
            this.writeExceptionJson(jsonObject, e);
        }
        return this.writeJson(jsonObject);
    }

    @PostMapping("/categoryUpStatus")
    @ResponseBody
    public String categoryUpStatus(SysAside sysAside) {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin sysAdmin = (SysAdmin) this.getAuth();
            jsonObject = this.systemService.categoryUpStatus(sysAside);
        } catch (Exception e) {
            this.writeExceptionJson(jsonObject, e);
        }
        return this.writeJson(jsonObject);
    }

    @GetMapping("/system-category-edit.html")
    public String categoryEdit(Model model, String asideId) {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin sysAdmin = (SysAdmin) this.getAuth();
            jsonObject = this.systemService.categoryEdit(asideId);
            model.addAttribute(KEY_DATA, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/system/system-category-edit";
    }

}
